package com.lzh.shijicloudstack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.shijicloudstack.common.R;
import com.lzh.shijicloudstack.common.ResultUtil;
import com.lzh.shijicloudstack.mapper.RoleMapper;
import com.lzh.shijicloudstack.model.dto.role.RolePageDto;
import com.lzh.shijicloudstack.model.entity.Role;
import com.lzh.shijicloudstack.service.RoleService;
import com.lzh.shijicloudstack.template.PageAbstractTemplate;
import com.lzh.shijicloudstack.utils.SqlUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luzhiheng
 * @date 2023/12/28 17:14
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private SqlUtil sqlUtil;

    @Override
    public Map<String, Object> pageList(RolePageDto dto) {
        Map<String, Object> otherQueryMap = new HashMap<>();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(dto.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pageList = baseMapper.pageList(this.queryMap);
                return pageList;
            }
        };
        return template.queryPage();
    }

    @Override
    public R<Boolean> delete(Long id) {
        baseMapper.deleteById(id);
        return ResultUtil.success(null);
    }

    @Override
    public Boolean updateRole(Role role) {
        Long id = role.getId();
        if (id != null) {
            // 编辑
            Role role1 = baseMapper.selectById(id);
            role1.setRoleName(role.getRoleName());
            role1.setRoleKey(role.getRoleKey());
            role1.setStatus(role.getStatus());
            role1.setRemark(role.getRemark());
            role1.setRoleSort(role.getRoleSort());
            sqlUtil.executeUpdateSql(sqlUtil.getUpdateSql(role1));
        } else {
            // 新增
            baseMapper.insert(role);
        }
        return true;
    }
}
